#include "ThinLensCamera.h"

void ThinLensCamera::setViewDistance(double d) {
	viewDistance = d;
}

void ThinLensCamera::setEyePosition(const Vector3 & eye, const Normal3 & dir, const Normal3 & up) {
	this->eye = eye;
	this->dir = dir;
	this->u = crossProduct(up, dir);
	this->v = crossProduct(dir, u);
}

void ThinLensCamera::setFocalDistance(double d) {
	focalDistance = d;
}

void ThinLensCamera::setLensRadius(Double r) {
	lensRadius = r;
}

void ThinLensCamera::generateRay(Ray & ray, const Vector2 & pixelSample, const Vector2 & apertureSample) const {
	Vector2 lp = apertureSample * lensRadius;
	Vector2 sp = pixelSample * (focalDistance / viewDistance);

	ray.origin = eye + lp.x * u + lp.y * v;
	ray.dir = (-sp.x - lp.x) * u + (sp.y - lp.y) * v + focalDistance * dir;
	ray.dir.normalize();
}
